package edu.uwm.universitydb.swing;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import edu.uwm.universitydb.model.Project;
import edu.uwm.universitydb.model.User;
import edu.uwm.universitydb.service.project.ProjectService;

public class ProjectTableModel extends DataTableModel {
	

	public ProjectTableModel(ProjectService service) {
		super(_colNames, _colTypes);
		_projectService = service;
		projects = _projectService.getAllProjects();
	}
	
	public ProjectTableModel(ProjectService service, User user, String ssn) {
		super(_colNames, _colTypes);
		_user = user;
		_ssn = ssn;
		_projectService = service;
		projects = _projectService.getProjectsByUser(user, ssn);
	}
	
	public ProjectService getService() {
		return _projectService;
	}
	
	@Override
	public int getRowCount() {
		return projects.size();
	}
	@Override
	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			return projects.get(row).getProjectNum();
		case 1:
			return projects.get(row).getProjectManager().getName();
		case 2:
			return projects.get(row).getDepartmentName();
		case 3:
			return projects.get(row).getSponsor();
		case 4:
			Date start  = projects.get(row).getStartDate();
			return _format.format(start);
		case 5:
			Date end = projects.get(row).getEndDate();
			return _format.format(end);
		case 6:
			return projects.get(row).getBudget();
		default:
			return "Error";
		}
	}
	
	public void refreshData() {
		if (_user == null) {
			projects = _projectService.getAllProjects();
		}
		else {
			projects = _projectService.getProjectsByUser(_user, _ssn);
		}
	}
	
	private static final String[] _colNames = new String[] {"Project No.","Managing Professor", "Department","Sponsor","Start Date","End Date","Budget"};
	private static final Class<?>[] _colTypes = new Class<?>[] {Integer.class, String.class, String.class, String.class, String.class, String.class, Integer.class};
	
	private final DateFormat _format = new SimpleDateFormat("yyyy-MM-dd");
	
	private static final long serialVersionUID = 2650564265612683458L;

	private List<Project> projects;

	private final ProjectService _projectService;
	
	private User _user = null;
	private String _ssn = "";
	
}
